package com.example.shopping_mall.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.shopping_mall.R;
import com.example.shopping_mall.bean.BrandManager;
import com.example.shopping_mall.bean.SecondLastBean;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

//品牌更多适配器
public class RecommendMoreAdapter extends RecyclerView.Adapter<RecommendMoreAdapter.BaseViewHolder>{
    Context context;
    private List<BrandManager> managers;
    private onMoreClickListener moreClickListener;

    public RecommendMoreAdapter(Context context, List<BrandManager> managers) {
        this.context = context;
        this.managers = managers;
    }

    public void setMoreClickListener(onMoreClickListener moreClickListener) {
        this.moreClickListener = moreClickListener;
    }

    public interface onMoreClickListener{
        void onMoreClick(int position);
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MoreHolder(inflate(parent, R.layout.recommend_more_item_list));
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.bindData(managers.get(position), position);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (moreClickListener!=null){
                    moreClickListener.onMoreClick(position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return null==managers?0:managers.size();
    }

    private View inflate(ViewGroup parent, int layout) {
        return LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
    }

    public abstract class BaseViewHolder<DATA> extends RecyclerView.ViewHolder {
        public BaseViewHolder(View itemView) {
            super(itemView);
        }

        public abstract void bindData(DATA data, int position);
    }

    class MoreHolder extends BaseViewHolder<BrandManager> {

        TextView morename;
        ImageView moreimage;
        TextView morestory;

        public MoreHolder(View itemView) {
            super(itemView);

            morename=itemView.findViewById(R.id.recommend_more_list_name);
            moreimage=itemView.findViewById(R.id.recommend_more_list_image);
            morestory=itemView.findViewById(R.id.recommend_more_list_story);
        }

        @Override
        public void bindData(BrandManager brandManager, int position) {
            morename.setText(brandManager.getName());
            morestory.setText(brandManager.getBrandStory());

            new Thread(new Runnable() {
                @Override
                public void run() {
                    URL imageurl = null;
                    Bitmap bitmap;
                    try {
                        imageurl = new URL(brandManager.getLogo());
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }
                    try {
                        HttpURLConnection conn = (HttpURLConnection) imageurl.openConnection();
                        conn.setDoInput(true);
                        conn.connect();
                        InputStream is = conn.getInputStream();
                        bitmap = BitmapFactory.decodeStream(is);
                        Handler handler = new Handler(Looper.getMainLooper());
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                moreimage.setImageBitmap(bitmap);
                            }
                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}
